package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.ScenicSport;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.ScenicSportService;
import com.example.toolproject.demo.util.FileUploadAndDownload;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName ScenicSportController
 * @Description TODO   景点控制层
 * @Author lvyongwei
 * @Date 2020-06-21 15:47
 * @Version 1.0
 **/
@CrossOrigin
@Controller
@RequestMapping("/scenicSport")
public class ScenicSportController {

    @Autowired
    private ScenicSportService scenicSportService;

    private Logger logger= LoggerFactory.getLogger(ScenicSportController.class);

    /**
     * 根据所选择的景点id获取景点信息
     * @param scenicSportId   景点id数组
     * @param request         获取token
     * @param pageNum         当前页
     * @param pageSize        一页显示多少个
     * @return
     */
    /*@PostMapping("/getChoiceScenicSport")
    @ResponseBody
    public ResultBean getChoiceScenicSport(HttpServletRequest request,@RequestBody String data){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        //获取字符串   形式如[1,2,3,4]
        String strScenicSportId=jsonObject.get("scenicSportId").toString();
        String str=strScenicSportId.substring(1,strScenicSportId.length()-1);
        String[] scenicSportId=str.split(",");
        Integer pageNum=jsonObject.getInt("pageNum");
        Integer pageSize=jsonObject.getInt("pageSize");

        *//**
         * 判断数组是否为空
         *//*
        if(scenicSportId.length==0 || StringUtils.isEmpty(scenicSportId)){
            return ResultBean.error("请获取选中的所有id信息");
        }
        //赋初值
        if(StringUtils.isEmpty(pageNum) || pageNum==0){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        *//**
         * 执行分页
         *//*
        PageHelper.startPage(pageNum,pageSize);
        //根据id数组获取景点信息
        List<ScenicSport> scenicSportList=scenicSportService.getChoiceScenicSport(scenicSportId);
        //获取分页信息
        PageInfo<ScenicSport> pageInfo=new PageInfo<ScenicSport>(scenicSportList);
        return ResultBean.success("成功获取数据信息",pageInfo);
    }*/


    /**
     * 根据用户获取用户选择的景点信息
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getChoiceScenicSport")
    @ResponseBody
    public ResultBean getChoiceScenicSport(HttpServletRequest request,Integer pageNum,Integer pageSize){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //赋初值
        if(StringUtils.isEmpty(pageNum) || pageNum==0){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        /**
         * 根据用户id获取用户所选择的景点信息
         */
        int userId=u.getId();
        List<String> scenicSportIds=scenicSportService.getAllScenicSportId(userId);
        if(StringUtils.isEmpty(scenicSportIds) || scenicSportIds.size()==0){
            return ResultBean.error("该用户没有选择任何信息");
        }
        /**
         * 执行分页
         */
        PageHelper.startPage(pageNum,pageSize);
        //根据id数组获取景点信息
        List<ScenicSport> scenicSportList=scenicSportService.getChoiceScenicSport(scenicSportIds.toArray(new String[]{}));
        //获取分页信息
        PageInfo<ScenicSport> pageInfo=new PageInfo<ScenicSport>(scenicSportList);
        return ResultBean.success("成功获取数据信息",pageInfo);
    }


    /**
     * 模糊查询 所有用户选择的信息
     * @param request
     * @param pageNum
     * @param pageSize
     * @param message
     * @return
     */
    @GetMapping("/getScenicSportByMessage")
    @ResponseBody
    public ResultBean getScenicSportByMessage(HttpServletRequest request,Integer pageNum,Integer pageSize,String message){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        /*if(Strings.isEmpty(message) || message==""){
            return ResultBean.error("请输入搜索条件");
        }*/
        //赋初值
        if(StringUtils.isEmpty(pageNum) || pageNum==0){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        /**
         * 根据用户id获取用户所选择的景点信息
         *//*
        int userId=u.getId();
        List<String> scenicSportIds=scenicSportService.getAllScenicSportId(userId);
        if(StringUtils.isEmpty(scenicSportIds) || scenicSportIds.size()==0){
            return ResultBean.error("该用户没有选择任何信息");
        }*/
        /**
         * 执行分页
         */
        PageHelper.startPage(pageNum,pageSize);
        //根据id数组以及搜索条件获取景点信息
        //List<ScenicSport> scenicSportList=scenicSportService.getScenicSportByMessage(scenicSportIds.toArray(new String[]{}),message,userId);
        List<ScenicSport> scenicSportList=scenicSportService.getScenicSportByMessage(message);
        //获取分页信息
        PageInfo<ScenicSport> pageInfo=new PageInfo<ScenicSport>(scenicSportList);
        if(pageInfo.getList().size()==0){
            return ResultBean.error("未查询到任何数据");
        }
        return ResultBean.success("成功获取数据信息",pageInfo);
    }


    /**
     * 查询所有景点信息
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getAllScenicSport")
    @ResponseBody
    public ResultBean getAllScenicSport(HttpServletRequest request,Integer pageNum,Integer pageSize){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //赋初值
        if(StringUtils.isEmpty(pageNum) || pageNum==0){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        /**
         * 执行分页
         */
        try{
            PageHelper.startPage(pageNum,pageSize);
            //获取所有景点信息
            List<ScenicSport> list=scenicSportService.getAllScenicSport2();
            //Collections.shuffle(list);
            PageInfo<ScenicSport> pageInfo=new PageInfo<ScenicSport>(list);
            return ResultBean.success("获取到旅游资源数据",pageInfo);
        }catch(Exception e){
            return ResultBean.error("没有查询到任何数据");
        }
    }

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 添加景点信息
     * @param file
     * @param scenicSport
     * @return
     */
    @PostMapping("/insertScenicSport")
    @ResponseBody
    public ResultBean insertScenicSport(@RequestParam("file") MultipartFile file,ScenicSport scenicSport,HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        String fileName="";
        if(file.getSize()==0){
            return ResultBean.error("请上传文件");
        }else{
            /**
             * 文件上传
             */
             fileName=fileUploadAndDownload.upload(file,"\\scenicSport");
        }
        scenicSport.setScenicSportImage("\\scenicSport\\"+fileName);
        try{
            scenicSportService.insertScenicSport(scenicSport);
            return ResultBean.success("添加景区信息成功");
        }catch (Exception e){
            return ResultBean.error("添加景区信息失敗");
        }
    }


    /**
     * 按照省份名称查询景点信息
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getAllScenicSportByCityName")
    @ResponseBody
    public ResultBean getAllScenicSportByCityName(HttpServletRequest request,Integer pageNum,Integer pageSize,String scenicCountry){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //赋初值
        if(StringUtils.isEmpty(pageNum) || pageNum==0){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        /**
         * 执行分页
         */
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<ScenicSport> list=null;
            if(scenicCountry.equals("全部")){
                list=scenicSportService.getAllScenicSport2();
            }else{
                list=scenicSportService.getAllScenicSportByCityName(scenicCountry);
            }
            //获取所有景点信息
            PageInfo<ScenicSport> pageInfo=new PageInfo<ScenicSport>(list);
            return ResultBean.success("获取到旅游资源数据",pageInfo);
        }catch(Exception e){
            return ResultBean.error("没有查询到任何数据");
        }
    }
}